{% extends 'TopxiaWebBundle:Default:layout.html.twig' %}

{% block content %}
  <div class="es-row-wrap container-gap">

    {% if blocks.home_top_banner %}
      <div class="homepage-feature homepage-feature-slides">
        <div class="cycle-pager"></div>
        {{ blocks.home_top_banner|raw }}
      </div>
    {% endif %}

    <ul class="homepage-category-nav">
      {% for category in categories %}
        <li><a href="{{ path('course_explore', {category:category.code|default(category.id)}) }}">{{ category.name }}</a></li>
      {% endfor %}
        <li><a href="{{ path('course_explore') }}">更多</a></li>
    </ul>

    <div class="row row-9-3">
      <div class="col-md-9">
        {# 推荐课程 #}
        {% set recommendCourses = data('RecommendCourses', {count:3}) %}
        {% if recommendCourses %}
          <div class="es-box">
            <div class="es-box-heading"><h2>推荐课程</h2>
              <a class="pull-right" href="{{ path('course_explore', {sort:'recommendedSeq'}) }}">更多&gt;</a>
            </div>
            <div class="es-box-body">
              {{ _self.course_grids(data('RecommendCourses',{ count:3 })) }}            
            </div>
          </div>
        {% endif %}

        {# 最新课程 #}
        {% set latestCourses = data('LatestCourses', {count:6}) %}
        {% if latestCourses %}
          <div class="es-box">
            <div class="es-box-heading"><h2>最新课程</h2>
              <a class="pull-right" href="{{ path('course_explore') }}">更多&gt;</a>
            </div>
            <div class="es-box-body">
              {{ _self.course_grids(data('LatestCourses',{ count:6 })) }}            
            </div>
          </div>
        {% endif %}

        {# 最新直播 #}
        {% if setting('course.live_course_enabled') %}
          {% set recentLiveCourses = data('RecentLiveCourses', {count:3}) %}
          {% if recentLiveCourses %}
            <div class="es-box">
              <div class="es-box-heading">
                <h2>最新直播</h2>
                <a class="pull-right" href="{{ path('live_course_explore') }}">更多&gt;</a>
              </div>
              <div class="es-box-body">
                {{ _self.course_grids(data('RecentLiveCourses',{ count:3 })) }}
              </div>
            </div>
          {% endif %}
        {% endif %}

        {# 最新资讯 #}
        {% set articles = data('LatestArticles',{'count':4}) %}
        {% if articles %}
          <div class="es-box news">
            <div class="es-box-heading">
              <h2>最新资讯</h2>
              <a class="pull-right" href="{{ path('article_show') }}">更多&gt;</a>
            </div>
            <div class="es-box-body">
              <ul class="row">
              {% for article in articles %}
                {% if article %}
                  <li class="col-md-6">
                    <em>{{ article.updatedTime|date('m-d H:i') }} </em>
                    <a href="{{ path('article_detail', {id:article.id}) }}"> <span>[{{ article.category.name }}]</span>{{ article.title }} </a>
                  </li>
                {% endif %}
              {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {# 推荐话题 #}
        {% set threads = data('EliteCourseThreadsByType',{'count':4, 'type':'discussion'}) %}
        {% if threads %}
          <div class="es-box topic">
            <div class="es-box-heading">
              <h2>课程讨论</h2>
            </div>
            <div class="es-box-body">
              <ul class="row">
                {% for thread in threads %}
                  {% if thread %}
                    <li class="col-md-6">
                      <div class="topic-item">
                        <h4>
                          <a href="{{ path('course_thread_show', {courseId:thread.courseId, id:thread.id}) }}">{{ thread.title|plain_text(28) }}</a>
                        </h4>
                        <div class="topic-body">
                          {% set user = thread.user %}
                          {% set latestPostUser = thread.latestPostUser %}
                          <img src="{{ default_path('avatar',thread.user.mediumAvatar, '') }}" class="pull-left">
                          <h3>{{ user.nickname }}</h3>
                          <p><a href="{{ path('course_show', {id:thread.course.id}) }}">{{ thread.course.title }}</a></p>
                        </div>

                        <p class="topic-bottom">
                          <span>{{ web_macro.user_link(latestPostUser) }}  </span>
              {{ thread.latestPostTime|smart_time }}回复
                          <span class="pull-right">共 {{ thread.postNum }} 个回复</span>
                        </p>
                      </div>
                    </li>
                  {% endif %} 
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {# 讲师团 #}
        {% set teachers = data('RecommendTeachers',{'count':'3'}) %}
        {% if teachers %}
          <div class="es-box teachers">
            <div class="es-box-heading">
              <h2>讲师团</h2>
              <a class="pull-right" href="{{ path('teacher') }}">更多&gt;</a>
            </div>
            <div class="es-box-body">
              <ul class="row teacher-grids">
                 {% for teacher in teachers %}
                  {% if teacher %}
                    <li class="col-sm-6 col-md-4">
                      <div class="teachers-item">
                        <a href="{{ path('user_show', {id:teacher.id}) }}">
                         <img class="img-circle" src="{{ default_path('avatar',teacher.mediumAvatar, '') }}">
                        </a>
                 
                          <span>{{ web_macro.user_link(teacher) }}</span>
                            <i> {{ teacher.title }}</i>
                            <p> {{ teacher.about|plain_text(50) }}</p>
                         </div>
                    </li>
                  {% endif %}
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {# vip #}
        {% if setting('vip.enabled') %}
          {% set levels = data('VipLevels',{'count':100}) %}
          {% if levels %}
            <div class="es-box vip">
              <div class="es-box-heading">
                <h2>会员专区</h2>
                <a class="pull-right" href="{{ path('vip') }}">更多&gt;</a>
              </div>
              <div class="es-box-body">
                <ul class="row vip-lists">
                  {% for level in levels %}
                    {% if level %}
                      <li class="col-sm-12">
                        <div class="vip-item">
                          <div class="row">
                            <div class="col-sm-4 col-md-4">
                              <a href="{{ path('vip_course_explore', {levelId:level.id}) }}">
                              <img class="img-responsive" src="{{ level.picture|default(asset('assets/img/default/vip-default.png')) }}" alt="{{ level.name }}">
                            </div>
                            </a>
                            <div class="col-sm-5 col-md-5">
                              <h3>{{ level.name }}</h3>
                              <p>{{ level.description|plain_text(40) }}</p>
                            </div>
                            <div class="vip-price col-sm-3 col-md-3">
                              <h4>¥&nbsp;{{ level.monthPrice }}</h4>
                              <h4><a href="{{ path('vip_buy') }}" class="btn btn-success">立即购买</a></h4>
                            </div>
                          </div>
                        </div>      
                      </li>
                    {% endif %}
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endif %}
        {% endif %}

         {# 小组 #}
        {% if setting('group').group_show|default(1) %}
          {% set groups = data('HotGroup', {'count':15}) %}
          {% if groups %}
            <div class="es-box hot-group">
              <div class="es-box-heading"><h2>最热小组</h2><a href="{{path('group_search_group')}}" class="pull-right">更多&gt;</a></div>
              <div class="es-box-body">
                 <ul class="list-unstyled">
                  {% for group in groups %}
                    {% if group.status=='open' %} 
                      <li class="col-md-4"> 
                        <div class="panel">
                           <div class="media">
                              <a href="{{path('group_show',{id:group.id})}}" title="{{group.title}}" class="pull-left">
                                {% if group.logo %}
                                <img src="{{file_path(group.logo)}}" alt="{{group.title}}">
                                {%else%}
                                <img src="{{asset('assets/img/default/group.png')}}" alt="{{group.title}}">
                                {%endif%}
                              </a>
                              <div class="media-body">
                                <p><a  href="{{path('group_show',{id:group.id})}}" title="{{group.title}}">{{group.title|sub_text(10)}}</a></p>
                                <div class="text-muted text-normal">
                                  {{group.memberNum}}个成员&nbsp;
                                  {{group.threadNum}}个话题
                                </div>
                              </div> 
                            </div>
                          </div>
                        </li>
                      {% endif %}
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endif %}
        {% endif %}

      </div>

      <div class="col-md-3">
        {# 在学课程 #}
        {% if app.user %}
          {{ render(controller('TopxiaWebBundle:Default:userlearning')) }}
        {% endif %}

        {# 热门标签 #}    
        {% set tags = data('Tags',{'count':5}) %}
        {% if tags %}
          <div class="es-box teachers">
            <div class="es-box-heading">
              <h2>热门标签</h2>
            </div>
            <div class="es-box-body">
              <div class="tag-list">
                {% for tag in tags %}
                  {% if tag %} 
                    <a href="{{ path('tag_show', {name:tag.name}) }}"> {{ tag.name }}</a>
                  {% endif %}
                {% endfor %}
              </div>
            </div>
          </div>
        {% endif %}

        {# 课程排行 #}
        {% set coursesRank = data('PopularCourses',{'count':'5', 'type':'studentNum'}) %}
        {% if coursesRank %}
          <div class="es-box courses-rank">
            <div class="es-box-heading">
              <h2>课程排行</h2>
            </div>
            <div class="es-box-body">
              <ul class="media-list">
                {% for course in coursesRank %}
                  {% if course %}
                    <li class="media">
                      <a href="{{ path('course_show', {id:course.id}) }}">
                        <div class="course-item">
                          <img src="{{ default_path('coursePicture',course.largePicture, 'large') }}"  class="img-responsive thumb pull-left">
                          <div class="course-title">{{ course.title }}</div>
                        </div>
                      </a>
                    </li>
                  {% endif %}
                {% endfor %}        
              </ul>
            </div>
          </div>
        {% endif %}

        {# 学员动态 #}
        {% set learns = data('PersonDynamic', {'count':5}) %}
        {% if learns %}
          <div class="es-box status-side">
            <div class="es-box-heading">
              <h2>学员动态</h2>
            </div>
            <div class="es-box-body">
              <ul class="media-list">
                {% for learn in learns %}
                  <li class="media">
                    <a class="pull-left" href="{{ path('user_show', {id:learn.user.id}) }}">
                      <img class="media-object" src="{{ default_path('avatar',learn.user.mediumAvatar, '') }}">
                    </a>
                    <div class="media-body">
                      <a href="{{ path('user_show', {id:learn.user.id}) }}">{{learn.user.nickname}}</a>
                      {% if learn.type == "finished_testpaper" %}
                      完成了考试 {{learn.properties.testpaper.name|plain_text(15)}}

                      {% elseif learn.type == "finished_homework" %}
                      完成了课程 {{learn.properties.course.title|default('')|plain_text(15)}} 课时 {{learn.properties.lesson.title|default('')|plain_text(15)}} 下的作业

                      {% elseif learn.type == "finished_exercise" %}
                      完成了课程 {{learn.properties.course.title|default('')|plain_text(15)}} 课时 {{learn.properties.lesson.title|default('')|plain_text(15)}} 下的练习

                      {% elseif learn.type == "become_student" %}
                      加入了课程 {{learn.properties.course.title|plain_text(15)}}
                      {% elseif learn.type == "learned_lesson" %}
                      完成了课时 {{learn.properties.lesson.title|plain_text(15)}}
                      {% endif %}
                    </div>
                  </li>
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {{ render(controller('TopxiaWebBundle:Default:latestReviewsBlock', {number:5})) }}

        {# 最热话题 #}
        {% if setting('group').group_show|default(1) %}
          {% set hotThreads = data('HotThreads', {'count':11}) %}
          {% if hotThreads %}
            <div class="es-box hot-threads">
              <div class="es-box-heading"><h2>最热话题</h2></div>
              <div class="es-box-body">
                <ul class="text-list">
                  {% for thread in hotThreads %}
                    {% if thread %}
                      <li style="border-bottom:none;background:url('{{asset('assets/img/default/triangle.png')}}') no-repeat 0 3px;padding-left:8px;padding-top:0px;margin-bottom:8px;"><a  href="{{path('group_thread_show',{id:thread.groupId,threadId:thread.id})}}">{{thread.title|sub_text(15)}}</a></li>
                    {% endif %}
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endif %}
        {% endif %}

      </div>
    </div>
    
  </div>
{% endblock %}

{% block contact %}
{% if contact %}
{% if contact.enabled == 1 %}
{% include 'TopxiaWebBundle:ContactFloat:contact.html.twig'   with {contact:contact} %}
{% endif %}
{% endif %}
{% endblock %}

{% macro course_grids(courses) %}
  {% set mode = mode|default('default') %}
  {% if courses %}
  <ul class="course-grids">
    {% for course in courses %}
      <li class="course-grid">
        <a href="{{ path('course_show', {id:course.id}) }}" class="grid-body">
          <img src="{{ default_path('coursePicture',course.largePicture, 'large') }}"  class="img-responsive thumb">
            {% if course.status == 'draft' %}
              <span class="label  label-warning course-status">未发布</span>
            {% elseif course.status == 'closed' %}
              <span class="label label-danger course-status">已关闭</span>
            {% endif %} 
            {% if course.serializeMode=='serialize' %}
              <span class="label label-success series-mode-label">更新中</span>
            {% elseif course.serializeMode=='finished' %}
              <span class="label label-warning series-mode-label">已完结</span>
            {% endif %}

            {% if course.type == 'live' %}
              {% set lesson = course['lesson']|default(null) %}
              {% if lesson and "now"|date("U") >= lesson.startTime and "now"|date("U") <= lesson.endTime %}
                <span class="label label-warning series-mode-label">正在直播中</span>
              {% else %}
                <span class="label label-success series-mode-label">直播</span>
              {% endif %}
            {% endif %}

          <span class="title">{{ course.title }}</span>
          {% if mode in ['default', 'teach'] %}

            {% if course.type == 'live' %}
              {% set lesson = course['lesson']|default(null) %}
              {% if lesson %}
                <span class="live-course-lesson metas">
                  <span class="text-success mrm">{{ lesson.startTime|date('n月j日 H:i') }} ~ {{ lesson.endTime|date('H:i') }}</span>
                  <span class="text-muted mrm">第{{ lesson.number }}课时</span>
                </span>
              {% endif %}
            {% endif %}

            <span class="metas clearfix">
              <span class="price-col">
                <span class="meta-label">价格</span>
                {% if course.price > 0 %}
                  <span class="price-num">{{ course.price }}元</span>
                {% else %}
                  <span class="price-num">免费</span>
                {% endif %}
              </span>

              {% if course.showStudentNumType == 'opened' %}
              <span class="student-col">
                <span class="meta-label">学生</span>
                <span class="student-num">{{ course.studentNum }}人</span>
              </span>
              {% endif %}

              <span class="review-col">
                {% if course.ratingNum > 0 %}
                  <span class="meta-label"><strong>{{ course.ratingNum }}</strong> 评价</span>
                  <span class="review-rating">
                    <span class="stars-{{ (course.rating)|number_format }}">&nbsp;</span>
                  </span>
                {% else %}
                  <span class="meta-label">无评价</span>
                  <span class="review-rating">
                    <span class="stars-0">&nbsp;</span>
                  </span>
                {% endif %}
              </span>

            </span>
          {% endif %}

          {% if mode in ['default'] %}
            {% set user = users[course.teacherIds|first]|default(null) %}
            {% if user %}
              <span class="teacher clearfix">
                <img src="{{ default_path('avatar',user.smallAvatar, '') }}" class="thumb">
                <span class="nickname ellipsis">{{ user.nickname }}</span>
                <span class="user-title ellipsis">{{ user.title }}</span>
              </span>
            {% endif %}
          {% endif  %}

          {% if mode in ['learn'] %}
            <div class="learn-status">
              {% if course.memberIsLearned %}
                <div class="progress">
                  <div class="progress-bar progress-bar-success" style="width: 100%;"></div>
                </div>
                <div class="action"><span class="btn btn-default btn-sm">查看课程</span></div>
              {% else %}
                <div class="progress">
                  <div class="progress-bar progress-bar-success" style="width: {{ percent(course.memberLearnedNum, course.lessonNum) }};"></div>
                </div>
                <div class="action"><span class="btn btn-primary btn-sm">继续学习</span></div>
              {% endif %}
            </div>
            {% if course.status == 'draft' %}
              <span class="label  label-warning course-status">未发布</span>
            {% elseif course.status == 'closed' %}
              <span class="label label-danger course-status">已关闭</span>
            {% endif %}       
          {% endif %}

          {% if mode == 'teach' %}
            {% if course.status == 'published' %}
              <span class="label label-success course-status">已发布</span>
            {% elseif course.status == 'draft' %}
              <span class="label  label-warning course-status">未发布</span>
            {% elseif course.status == 'closed' %}
              <span class="label label-danger course-status">已关闭</span>
            {% endif %}
          {% endif %}

        </a>
      </li>
    {% endfor %}
  </ul>
  {% else %}
    <div class="empty">暂无课程。</div>
  {% endif %}
{% endmacro %}